<?php
//活动表

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Casts\Attribute;

class FollowModel extends Model
{
    use HasFactory, SoftDeletes;

    // 关注程度：10尝试型，20轻度关注，30中度关注，40重点关注
    const TRY_LEVEL = 10;
    const LIGHT_LEVEL = 20;
    const MEDIUM_LEVEL = 30;
    const MAJOR_LEVEL = 40;

    const LEVELS = [
        SELF::TRY_LEVEL => '尝试型',
        SELF::LIGHT_LEVEL => '轻度关注',
        SELF::MEDIUM_LEVEL => '中度关注',
        SELF::MAJOR_LEVEL => '重点关注',
    ];

    const IS_MONITOR = 1;
    const IS_NOT_MONITOR = 2;

    const MONITORS = [
        SELF::IS_MONITOR => '是',
        SELF::IS_NOT_MONITOR => '否'
    ];

    protected $table = 'follow';

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    // 访问器
    protected function highestPrice(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }

    protected function followPrice(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }

    protected function unfollowPrice(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }

    protected function currentPrice(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }

    protected function lowestPrice(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }

    protected function highestRate(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 10000,
        );
    }

    protected function lowestRate(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 10000,
        );
    }

    protected function rate(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 10000,
        );
    }

    public function strategy ()
    {
        return $this->belongsTo(StrategyModel::class, 'strategy_id', 'id');
    }
}
